package com.king.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql._
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction}
import org.apache.spark.sql.types._


object SparkSQL06_read_write {
  def main(args: Array[String]): Unit = {
    //新的起点: SparkSession

    val sparkConf: SparkConf = new SparkConf().setAppName("SparkSQL").setMaster("local[*]")

    val spark: SparkSession =
          SparkSession.builder()
            //.appName("SparkSQL")
            //.master("local[*]")
            .config(sparkConf)
            .getOrCreate()

    //写代码不管用不用都导入。
    import spark.implicits._

    //通用的读取
    // 默认的读取格式: parquet
    //val df: DataFrame = spark.read.load("data/user.json")
    val df: DataFrame = spark.read.format("json").load("data/user.json")
    df.show()


    //通用的保存
    // 默认的保存格式: parquet
    //df.write.save("output")
    df.write.mode(SaveMode.Append).format("json").save("output")


    spark.stop()

  }
}
